<?php

abstract class Gtux_Model_Gateway_Abstract
{
  /**
   * @var Zend_Db_Table_Abstract
   */
  protected $_dbTable;
  /**
   * @var Zend_Log
   */
  protected $_logger;
  
  public function __construct()
  {
    $this->_logger = Zend_Registry::get('logger');
  }
  
  /**
   * Set the DbTable to Gateway
   *
   * @param string|Zend_Db_Table_Abstract $dbTable
   * @return Gtux_Model_Gateway_Abstract
   */
  public function setDbTable($dbTable) 
  {
    if (is_string($dbTable)) 
    {
      $dbTable = new $dbTable();
    }
    if (!$dbTable instanceof Zend_Db_Table_Abstract) 
    {
      throw new LogicException('Invalid table data gateway provided');
    }
    $this->_dbTable = $dbTable;
    return $this;
  }

  /**
   * Buscar en la bd
   * 
   * @param int $id
   * @param Gtux_Model_Abstract $model
   * @return Zend_Db_Table_Row_Abstract
   */
  public function find($id, Gtux_Model_Abstract $model)
  {
    $rs = $this->getDbTable()->find($id);
    if (0 == count($rs)) 
    {
      return;
    }
    $row      = $rs->current();
    $rowArray = $row->toArray();
    foreach ($rowArray as $column => $value) 
    {
      $model->$column = $value;
    }    
    return $row;
  }
  
  /**
   * Return DbTable model
   *
   * @return Zend_Db_Table_Abstract
   */
  abstract public function getDbTable();
      
  /**
   * Guarda la data del modelo en la base de datos
   *
   * @param Gtux_Model_Abstract $model
   */
  abstract public function save(Gtux_Model_Abstract $model);
  
  /**
   * Returns an array with all objects of model 
   *
   * @return array
   */
  abstract public function fetchAll();

  /**
   * Elimina un registro de la base de datos
   *
   * @param Gtux_Model_Abstract $model
   * @return int
   */
  abstract public function delete(Gtux_Model_Abstract $model);
  
  /**
   * Inserta un nuevo registro
   *
   * @param Gtux_Model_Abstract $model
   */
  abstract public function insert(Gtux_Model_Abstract $model);
  
  /**
   * Actualiza el registro
   *
   * @param Gtux_Model_Abstract $model
   */
  abstract public function update(Gtux_Model_Abstract $model);
}

?>